<?php
include("./include/config.inc.php");
include("./class.php");
if(!isset($_SESSION['s_loggedId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else 
{
  if(isset($_SESSION['s_edit']) && $_SESSION['s_edit'] == 0 && isset($_SESSION['s_add']) && $_SESSION['s_add'] == 0)
  {
  	header("Location:index.php");
  	exit();
  }
	$success = 0;
	$isEdit  = 0;
	$checkDate = 0;
	$transactionArr = 0;
	$exchangeId = 0;
	$exchangeSelectedArr = array();
	$callOfInsertClass = new database();
	if(isset($_REQUEST['btnConfirm']))
	{
		$softId      = isset($_REQUEST['softId']) ? $_REQUEST['softId'] : ""; 
		$password    = isset($_REQUEST['password']) ? $_REQUEST['password'] : ""; 
		$custName    = isset($_REQUEST['custName']) ? $_REQUEST['custName'] : ""; 
		$mobileNo    = isset($_REQUEST['mobileNo']) ? $_REQUEST['mobileNo'] : ""; 
		$stateId     = isset($_REQUEST['stateId']) ? $_REQUEST['stateId'] : ""; 
		$cityId      = isset($_REQUEST['cityId']) ? $_REQUEST['cityId'] : ""; 
		$billAmount  = isset($_REQUEST['billAmount']) && ($_REQUEST['billAmount'] != "") ? $_REQUEST['billAmount'] : 0; 
		$payVia      = isset($_REQUEST['payVia']) ? $_REQUEST['payVia'] : 0; 
		$bankNm      = isset($_REQUEST['bankNm']) ? $_REQUEST['bankNm'] : ""; 
		$accNm       = isset($_REQUEST['accNm']) ? $_REQUEST['accNm'] : ""; 
		$notes       = isset($_REQUEST['notes']) ? $_REQUEST['notes'] : ""; 
		$startDate   = $_REQUEST['startDateYear']."-".$_REQUEST['startDateMonth']."-".$_REQUEST['startDateDay'];
		$expiryDate  = isset($_REQUEST['endDate']) && ($_REQUEST['endDate'] != "") ? $_REQUEST['endDate'] : date("Y-m-d",strtotime("+1 days"));
		$period      = isset($_REQUEST['period']) ? $_REQUEST['period'] : ""; 
		$holderId    = isset($_REQUEST['holderId']) && ($_REQUEST['holderId'] != "") ? $_REQUEST['holderId'] : 0;
		$resellerId  = isset($_REQUEST['resellerId']) ? $_REQUEST['resellerId'] : 0;
		
	  if(isset($_REQUEST['softId']) && $_REQUEST['softId'] != "")
	  {
	  	$status      = "";
	  	if($period == 'D')
	  	{
	  		$status = 'D';
	  		$period = "Demo";
	  	}
	  	else
	  	{
	  		$status = 'C';
	  	}
	  	/* insert into state START:: */
	  	if($stateId == "")
	  	{
	  		if(isset($_REQUEST['state']) && $_REQUEST['state'] != "")
	  		{
	  			$table = "state";
	  			$rows = "stateName";
	  			$stateArr = array($_REQUEST['state']);
	  			$stateInsert = $callOfInsertClass->insert($table,$stateArr,$rows);
	  			$stateId = mysql_insert_id();
	  	  }
	  	}
	  	/* insert into state END:: */
	  	/* insert into city START:: */
	  	if($cityId == "")
	  	{
	  		if(isset($_REQUEST['city']) && $_REQUEST['city'] != "")
	  		{
	  			$table = "city";
	  			$rows  = "stateId,cityName";
	  			$stateArr = array($stateId,$_REQUEST['city']);
	  			$stateInsert = $callOfInsertClass->insert($table,$stateArr,$rows);
	  			$cityId = mysql_insert_id();
	  	  }
	  	}
	  	/* insert into city END:: */
	  	
	  	/* insert into Holder START:: */
	  	if($holderId == "")
	  	{
	  		if(isset($_REQUEST['holderNm']) && $_REQUEST['holderNm'] != "")
	  		{
	  			$table = "accounthoder";
	  			$rows = "holderName";
	  			$holderArr = array($_REQUEST['holderNm']);
	  			$holderInsert = $callOfInsertClass->insert($table,$holderArr,$rows);
	  			$holderId = mysql_insert_id();
	  	  }
	  	}
	  	/* insert into Holder END:: */
	  	
	  	/* Insert into Client Start::*/
	  	if($resellerId == 0)
	  	{
	  	  $table = "client";
	  	  $rows  = "clientName,resellerId,mobile1,cityId,stateId";
	  	  $clientArr = array($custName,$resellerId,$mobileNo,$cityId,$stateId);
	  	  $queryClient = $callOfInsertClass->insert($table,$clientArr,$rows);
	  	  $clientInsertId = mysql_insert_id();
	  	}
	  	else{
	  		$clientInsertId = 0;
	  	}
	  	/* Insert into Client End::*/
	  
	  	if(isset($_REQUEST['transactionId']) && $_REQUEST['transactionId'] > 0 
	  	&& isset($_REQUEST['renew']) && $_REQUEST['renew'] != 1
	  	&& isset($_REQUEST['exchangeVal']) && $_REQUEST['exchangeVal'] != 1)
			{
		  	$table = "transmaster";
        $valuesArray['resellerId']     = $resellerId;
        $valuesArray['date']           = $startDate;
        $valuesArray['clientId']       = $clientInsertId;
        $valuesArray['softwareId']     = $softId;
        $valuesArray['password']       = $password;
        $valuesArray['period']         = $period;
        $valuesArray['billAmount']     = $billAmount;
        $valuesArray['payViaId']       = $payVia ;
        $valuesArray['expiryDate']     = $expiryDate;
        $valuesArray['accountHoderId'] = $holderId ;
        $valuesArray['notes']          = $notes ;
        $valuesArray['status']         = $status ;
       
        $where = "transactionId = ".$_REQUEST['transactionId']." ";
	      $query = $callOfInsertClass->update($table,$valuesArray,$where);       
	      //header("location:report.php");
	      $transInsertId = $_REQUEST['transactionId'];

	      $delQuery = mysql_query("DELETE FROM transdetail WHERE transactionId = ".$_REQUEST['transactionId']." "); 
		  	if(isset($_REQUEST['exchangeId']))
		  	{
		  	  $table = "transdetail";
		  	  $rows  = "transactionId,exchangeId";
		  	  foreach($_REQUEST['exchangeId'] as $count => $exchange)
		  	  {
		  	  	$transArr   = array($transInsertId,$exchange);
		  	  	$queryTrans = $callOfInsertClass->insert($table,$transArr,$rows);
		  	  }
		  	}
		  	if($queryTrans[1] > 0)
		  	{
		  		$success = 2;
		  	}
		  	
		  }
			else
			{
		  	/* Insert into Client Start::*/
		  	$table = "transmaster";
		  	$rows  = "resellerId,date,clientId,softwareId,password,period,billAmount,payViaId,expiryDate,accountHoderId,notes,status";
		  	$transMArr = array($resellerId,$startDate,$clientInsertId,$softId,$password,$period,$billAmount,$payVia,$expiryDate,$holderId,$notes,$status);
		  	$queryTrans = $callOfInsertClass->insert($table,$transMArr,$rows);
		  	$transInsertId = mysql_insert_id();
		  	/* Insert into Client End::*/
		  	/* Insert into Transdetail Start::*/
		  	if(isset($_POST['exchangeId']))
		  	{
		  	  $table = "transdetail";
		  	  $rows  = "transactionId,exchangeId";
		  	  foreach($_POST['exchangeId'] as $count => $exchange)
		  	  {
		  	  	$transArr   = array($transInsertId,$exchange);
		  	  	$queryTrans = $callOfInsertClass->insert($table,$transArr,$rows);
		  	  }
		  	}
		  	if($queryTrans[1] > 0)
		  	{
		  		$success = 1;
		  	}
		  	/* Insert into Transdetail End::*/
	  	}
	  }
		
	}
	/* Insert into exchanges Start::*/
	/* Select Data In Edit Start:::*/
	$renew    = isset($_GET['renew']) ? $_GET['renew'] : 0; 
	$exchangeVal = isset($_GET['exchange']) ? $_GET['exchange'] : 0; 
	if(isset($_GET['transactionId']) && $_GET['transactionId'] > 0)
	{
	  if(isset($_SESSION['s_edit']) && $_SESSION['s_edit'] == 0)
    {
      $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
      header("Location:index.php");
    }
    else
    {
		  $table = "transmaster
		            LEFT JOIN client ON  client.clientId = transmaster.clientId
		            LEFT JOIN state ON client.stateId = state.stateId
                LEFT JOIN city  ON  client.cityId = city.cityId
		            LEFT JOIN payvia ON payvia.payViaId = transmaster.payViaId
		            LEFT JOIN accounthoder ON  accounthoder.accountHoderId = transmaster.accountHoderId
		            LEFT JOIN resellermaster ON resellermaster.resellerMasterId = transmaster.resellerId";
		  $fields = " transmaster.transactionId, transmaster.date, transmaster.resellerId,transmaster.clientId, transmaster.softwareId, transmaster.password, transmaster.period,
		            	transmaster.billAmount, transmaster.payViaId, client.clientName, city.cityName, state.stateName, client.mobile1, client.stateId, client.cityId, transmaster.expiryDate, transmaster.accountHoderId,
		            	transmaster.notes, accounthoder.holderName, accounthoder.accountHoderId";
		  $where = " transmaster.transactionId = ".$_GET['transactionId'];
		  $selectTransaction = $callOfInsertClass->selectQuery($table,$fields,$where);
		  $transactionArr = array();
		  while($transactionRes = mysql_fetch_object($selectTransaction))
		  {
		  	$isEdit = 1;
		  	$transactionArr['transactionId'] = $transactionRes->transactionId;
		  	$transactionArr['resellerId']    = $transactionRes->resellerId;
		  	$transactionArr['softwareId']    = $transactionRes->softwareId;
		  	$transactionArr['password']      = $transactionRes->password;
		  	$transactionArr['period']        = $transactionRes->period;
		  	if($renew == 1 || $exchangeVal == 1)
		  	{
		  	  $transactionArr['date']        = date("Y-m-d");
		  	}
		  	else
		  	{
		  	  $transactionArr['date']        = $transactionRes->date;
		  	}
		  	$transactionArr['expiryDate']    = $transactionRes->expiryDate;
		  	$transactionArr['billAmount']    = $transactionRes->billAmount;
		  	if($renew == 1)
		  	{
		  	  $transactionArr['payViaId']      = 1;
		  	}
		  	else
		  	{
		  		$transactionArr['payViaId']      = $transactionRes->payViaId;
		  	}
		  	$transactionArr['accountHoderId']= $transactionRes->accountHoderId;
		  	$transactionArr['holderName']    = $transactionRes->holderName;
		  	$transactionArr['clientName']    = $transactionRes->clientName;
		  	$transactionArr['resellerId']    = $transactionRes->resellerId;
		  	$transactionArr['mobile1']       = $transactionRes->mobile1;
		  	$transactionArr['cityId']        = $transactionRes->cityId;
		  	$transactionArr['stateId']       = $transactionRes->stateId;
		  	$transactionArr['stateName']     = $transactionRes->stateName;
		  	$transactionArr['cityName']      = $transactionRes->cityName;
		  	$transactionArr['notes']         = $transactionRes->notes;
		  	$transactionId = $transactionRes->transactionId;
		  	$table = " transdetail
		  	           JOIN transmaster ON  transmaster.transactionId	 = transdetail.transactionId
		  	           JOIN exchanges ON exchanges.exchangeId = transdetail.exchangeId";
		  	$fields = " transdetail.exchangeId";
		  	$where = "transmaster.transactionId	 = ".$transactionRes->transactionId;
		  	$selectexchange = $callOfInsertClass->selectQuery($table,$fields,$where);
		  	while($exchageRes = mysql_fetch_object($selectexchange))
		  	{
  	  		$exchangeSelectedArr[] = $exchageRes->exchangeId;
		    }
		  }
	  }
	}
	/* Select Data In Edit End:::*/
  $table = "exchanges";
  $fields = "exchangeId, exchange";
  $where = "exchangeId !=".$exchangeId;
  if(isset($_GET['exchange']) && $_GET['exchange'] == 1)
	{
		$checkDate = 1;
		foreach($exchangeSelectedArr as $count => $exchangeId)
    {
      $where .= " AND exchangeId !=".$exchangeId;
    }
    $selectExchange = $callOfInsertClass->selectQuery($table,$fields,$where);
	}
	else
	{
		$selectExchange = $callOfInsertClass->selectQuery($table);
	}
	$exchangeArr = array();
	$exchangeArr['exchangeId'][0] = 0;
	$exchangeArr['exchange'][0]   = "";
	$exCount = 0;
	while($exchange = mysql_fetch_object($selectExchange))
	{
	  $exchangeArr['exchangeId'][$exCount] = $exchange->exchangeId;
	  $exchangeArr['exchange'][$exCount]   = $exchange->exchange;
	  $exCount++;
	}
	/* Insert into exchanges End::*/
	
	/* Insert into reseller Start::*/
	$table = " resellermaster";
	$selectReseller = $callOfInsertClass->selectQuery($table);
	$resellerArr = array();
	$resellerArr['resellerMasterId'][0] = 0;
	$resellerArr['resellerName'][0]   = "";
	$resellerCount = 0;
	while($resellerRow = mysql_fetch_object($selectReseller))
	{
		$resellerArr['resellerMasterId'][$resellerCount] = $resellerRow->resellerMasterId;
		$resellerArr['resellerName'][$resellerCount]   = $resellerRow->resellerName;
		$resellerCount++;
	}
	/* Insert into reseller End::*/
	
		/* Insert into payvia Start::*/
	$table = "payvia";
	$selectPayVia = $callOfInsertClass->selectQuery($table);
	$payViaArr = array();
	$payViaArr['payViaId'][0] = 0;
	$payViaArr['payVia'][0]   = "";
	$payViaCount = 0;
	while($payViaRow = mysql_fetch_object($selectPayVia))
	{
		$payViaArr['payViaId'][$payViaCount] = $payViaRow->payViaId;
		$payViaArr['payVia'][$payViaCount]   = $payViaRow->payVia;
		$payViaCount++;
	}
	/* Insert into payvia End::*/
  include("./bottom.php");
  $smarty->assign("success",$success);
  $smarty->assign("checkDate",$checkDate);
  $smarty->assign("resellerArr",$resellerArr);
  $smarty->assign("exchangeArr",$exchangeArr);
  $smarty->assign("payViaArr",$payViaArr);
  $smarty->assign("isEdit",$isEdit);
  $smarty->assign("transactionArr",$transactionArr);
  $smarty->assign("exchangeSelectedArr",$exchangeSelectedArr);
  $smarty->assign("renew",$renew);
  $smarty->assign("exchangeVal",$exchangeVal);
  $smarty->assign("currentDate",date('Y-m-d'));
  $smarty->assign("expiryDate",date("Y-m-d",strtotime("+1 days")));
  $smarty->display("confirm.tpl");	
}
?>